//
// 下载职工档案 2017/5/31
//
//

#import "DlPubEmployee.h"
#import "Networking.h"
#import "InitDbTable.h"
#import <TMCache.h>

@implementation DlPubEmployee

-(instancetype)init{
    
    NSString *url = AppServiceConfig.GetServiceUrl;
    self.downloadUrl  = [NSString stringWithFormat:@"%@/%@?token=%@",url,@"getemployee",[[TMCache sharedCache] objectForKey:@"deviceToken"]];
    return self;
}

-(void) startDownload:(void(^)())callback{
    NSLog(@"开始 下载职工");
    
    
    [Networking receiveDataWithURLString:self.downloadUrl method:@"GET" body:nil Block:^(NSData *tempData) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:tempData options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *arr = [[NSMutableArray alloc]init];
        InitDbTable* initdb = [[InitDbTable alloc]init];
        
        
        NSLog(@"下载成功，共%lu条职工数据",(unsigned long)[dic count]);
        
        for (NSDictionary *diction in dic) {
            Model_Pub_Employee *model =[[Model_Pub_Employee alloc]initWithDictionary:diction];
            [arr addObject:model];
        }
        
        NSLog(@"正在清除原有职工数据");
        [initdb deletePubEmployee];
        
        NSLog(@"正在写入职工数据");
        [initdb insertPubEmployeeArray:arr];
        
        if(callback){
            callback();
        }
        
        NSLog(@"完成 下载职工");
        
    }];
    
}

@end

